package com.w2e.travel.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.w2e.travel.entity.Contents;
import com.w2e.travel.mapper.ContentsMapper;
import com.w2e.travel.service.IContentsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.w2e.common.utils.Result;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyyl
 * @since 2024-07-01
 */
@RestController
@RequestMapping("/contents")
@RequiredArgsConstructor
public class ContentsController {

    private final ContentsMapper contentsMapper;
    private final IContentsService contentsService;

    @PostMapping("/content")
    public Result addContent(@RequestBody Contents contents){
        if (contents.getContentId() == null || contents.getContentId() == 0){
            contentsMapper.insert(contents);
        }else{
            contents.setIsUse(1);
            contentsMapper.updateById(contents);
        }
        return new Result(200,contents.getContentId(),"");
    }


    @GetMapping("/setUnUse")
    public void setUnUse(@RequestParam("travelogueId") Integer id){
        contentsMapper.setUnUse(id);
    }


    @GetMapping("/getAll")
    public Result getAllByTid(@RequestParam("travelogueId")Integer id){
        LambdaQueryWrapper<Contents> lqw = new LambdaQueryWrapper();
        lqw.eq(Contents::getTravelogueId,id);
        lqw.eq(Contents::getIsUse,1);
        List<Contents> list = contentsMapper.selectList(lqw);
        return new Result(200,list,"");
    }


}
